﻿// ********************************************
// ClassName  ：  {filename}
// Author        ：  
// Company     ： 智测心控
// Version        ：V1.0
// CreateTime ： {date}
// Description ：  
// ********************************************

using DAL.DB;
using DAL.Model;
using DataCenter;
using DcpServer.Common.Common;
using Newtonsoft.Json;
using System;
using System.Linq;

namespace WcfService.SystemSettingService
{
    /// <summary>
    /// 系统设置服务实现类
    /// </summary>
    public class SystemSettingService : ISystemSettingService
    {
        /// <summary>
        /// 添加系统设置
        /// </summary>
        /// <param name="str">str</param>
        /// <returns>系统设置</returns>
        public string SystemSettingAdd(string str)
        {
            using (var db = new YFrameContext())
            {
                try
                {
                    SystemSetting settingInfo = JsonConvert.DeserializeObject<SystemSetting>(str);
                    settingInfo.Id = Guid.NewGuid();
                    settingInfo.CreateTime = DateTime.Now;
                    db.SystemSetting.Add(settingInfo);
                    var json = new
                    {
                        isTrue = db.SaveChanges() >= 0 ? true : false,
                        msg = db.SaveChanges() >= 0 ? "添加成功" : "添加失败",
                    };
                    return JsonConvert.SerializeObject(json);
                }
                catch (Exception ex)
                { 
                
                }
            }

            return "";
        }

        /// <summary>
        /// 系统设置查询
        /// </summary>
        /// <param name="macId"></param>
        /// <param name="patid"></param>
        /// <returns></returns>
        public string SystemSettingSelect()
        {
            using (var db = new YFrameContext())
            {
                string macAddress = CommonHelper.GetLocalMAC();
                //PatientInfo patinfo = DcpContext.Instance.CurentPatient;
                var patid = Guid.Empty;
                if (DcpContext.Instance.CurentPatient != null)
                {
                    patid = DcpContext.Instance.CurentPatient.Id;
                }
                var systemSettingInfo = db.SystemSetting.Where(u => u.LocalMacAddress == macAddress && u.PatientId == patid).ToList().FirstOrDefault();
                string jsonStr = string.Empty;
                if (systemSettingInfo != null)
                {
                    jsonStr = JsonConvert.SerializeObject(systemSettingInfo);
                }
                else
                { 
                   var systeminfo = db.SystemSetting.Where(u => u.LocalMacAddress == macAddress).ToList().FirstOrDefault();
                   jsonStr = JsonConvert.SerializeObject(systeminfo);
                }
                return jsonStr;
            }
        }

        /// <summary>
        /// 修改系统设置
        /// </summary>
        /// <param name="str">str</param>
        /// <returns>系统设置</returns>
        public string SystemSettingUpdate(string str)
        {
            using (var db = new YFrameContext())
            {
                SystemSetting systemInfo = JsonConvert.DeserializeObject<SystemSetting>(str);
                //var systemSettingInfo = db.SystemSetting.Find(systemInfo.Id);
                var systemSettingInfo = db.SystemSetting.Where(u => u.LocalMacAddress == systemInfo.LocalMacAddress && u.PatientId == systemInfo.PatientId).ToList().FirstOrDefault();
                systemSettingInfo.LocalMacAddress = systemInfo.LocalMacAddress;
                systemSettingInfo.PatientId = systemInfo.PatientId;
                systemSettingInfo.HeartSource = systemInfo.HeartSource;
                systemSettingInfo.EcgWaveSpeed = systemInfo.EcgWaveSpeed;
                systemSettingInfo.EcgGain = systemInfo.EcgGain;
                systemSettingInfo.Spo2WaveSpeed = systemInfo.Spo2WaveSpeed;
                systemSettingInfo.Spo2Gain = systemInfo.Spo2Gain;
                systemSettingInfo.PulseVolume = systemInfo.PulseVolume;
                systemSettingInfo.Mode = systemInfo.Mode;
                systemSettingInfo.TimeSpan = systemInfo.TimeSpan;
                systemSettingInfo.RespWaveSpeed = systemInfo.RespWaveSpeed;
                systemSettingInfo.RespGain = systemInfo.RespGain;
                systemSettingInfo.ModifiedTime = DateTime.Now;

                var result = db.SaveChanges();

                var json = new
                {
                    isTrue = result >= 0,
                    msg = result >= 0 ? "修改成功" : "修改失败",
                };
                return JsonConvert.SerializeObject(json);
            }
        }
    }
}
